package math;

public class Range
{
    public double min, max;
	
    public Range()
    {
    	this.reset();
    }
    public Range(double min, double max)
    {
    	this.min = Math.min(min, max);
    	this.max = Math.max(min, max);
    }
    public void reset()
    {
    	this.min = Double.MAX_VALUE;
    	this.max = Double.MIN_VALUE;
    }
    public void combineWith(Range arg)
    {
    	this.addPoint(arg.max);
    	this.addPoint(arg.min);
    }
    public void addPoint(double point)
    {
    	this.min = Math.min(point, this.min);
    	this.max = Math.max(point, this.max);
    }
    public double getLength()
    {
    	return this.max-this.min;
    }
    public double getAvg()
    {
    	return (this.max+this.min)/2;
    }
    public double overlap(Range arg)
    {
    	double amt = this.difference(arg);
    	if(amt<=0)
    	{
    	    return 0;
    	}
    	else
    	{
    	    if(this.getAvg()>arg.getAvg())
    	    {
    		amt*=-1;
    	    }							  
    	}
    	return amt;
    }
    public double difference(Range arg)
    {
    	return (this.getLength()+arg.getLength())-(Math.max(this.max, arg.max)-Math.min(this.min, arg.min));
    }
    public double intervalDistance(Range arg)
    {
	if (this.min < arg.min) 
	{
	    return arg.min - this.max;
	}
	return this.min - arg.max;
    }
}